package com.lei.sqlGoodsJsp.servlet;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Optional;

import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.lei.sqlGoodsJsp.constant.UserConstant;
import com.lei.sqlGoodsJsp.entity.Goods;
import com.lei.sqlGoodsJsp.entity.Page;
import com.lei.sqlGoodsJsp.entity.User;
import com.lei.sqlGoodsJsp.service.GoodsService;
import com.lei.sqlGoodsJsp.service.Impl.GoodsServiceImpl;

public class GoodsPageServlet extends HttpServlet {
	static GoodsService goodsService = new GoodsServiceImpl();
	private Long count = 1L;

	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		//session中是否存在UserConstant.USER_INFO key
		req.setCharacterEncoding("UTF-8");
		resp.setCharacterEncoding("UTF-8");
		HttpSession session = req.getSession();
		if(req.getSession().getAttribute(UserConstant.USER_INFO)==null) {
			resp.sendRedirect("sql/login.jsp");
			return;
		}
		String method = req.getParameter("method");
		System.out.println(method);
		if ("add".equals(method)) {
			add(req, resp);
			resirectList(resp);
		} else if ("update".equals(method)) {
			edit(req, resp);
			resirectList(resp);
		} else if ("toUpdate".equals(method)) {
			toUpdate(req, resp);
		} else if ("delete".equals(method)) {
			delete(req);
			resirectList(resp);
		} else if ("list".equals(method)) {
			list(req,resp);
		} else {
			resirectList(resp);
		}
	}
	private void resirectList(HttpServletResponse res) throws IOException {
		res.sendRedirect("GoodsPageServlet?method=list");
	}
	
	private void add(HttpServletRequest req, ServletResponse res) throws ServletException, IOException {
		String shopNumber = req.getParameter("shopNumber");
		String shopName = req.getParameter("shopName");
		String deleteLogo = req.getParameter("deleteLogo");
		String creator =req.getParameter("creator");
		
		Goods goods = new Goods();
		goods.setShopNumber(shopNumber);
		goods.setShopName(shopName);
		goods.setDeleteLogo(deleteLogo);
		goods.setCreator((String)req.getSession().getAttribute(UserConstant.USER_INFO));
		goods.setMender((String)req.getSession().getAttribute(UserConstant.USER_INFO));
//		add(Goods)方法未为类型Goods定义
		goodsService.add(goods);
	}
	private void edit(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
		Long id = Long.valueOf(req.getParameter("id"));
		String shopNumber = req.getParameter("shopNumber");
		String shopName = req.getParameter("shopName");
		String deleteLogo = req.getParameter("deleteLogo");
		
		Goods goods = new Goods();
		goods.setId(id);
		goods.setShopNumber(shopNumber);
		goods.setShopName(shopName);
		goods.setDeleteLogo(deleteLogo);
		goods.setCreator((String)req.getSession().getAttribute(UserConstant.USER_INFO));
		goods.setMender((String)req.getSession().getAttribute(UserConstant.USER_INFO));
		goodsService.update(goods);
	}
	private void toUpdate(ServletRequest req, ServletResponse res) throws ServletException, IOException {
		Long id = Long.valueOf(req.getParameter("id"));
		System.out.println(id);
		req.setAttribute("myShop",goodsService.getById(id));
		req.getRequestDispatcher("sql/edit.jsp").forward(req, res);

	}
	private void delete(ServletRequest req) {
		Long id = Long.valueOf(req.getParameter("id"));
		System.out.println(id);
		goodsService.delete(id);
	}

	private void list(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
		req.setAttribute("myShop", ShopPage(req));
		req.getRequestDispatcher("sql/shop.jsp").forward(req, resp);
	}
	private Page<Goods> ShopPage(HttpServletRequest req) throws ServletException, IOException {
		
		String currentPage = req.getParameter("currentPage");
		int intCurrentPage = Optional.ofNullable(currentPage).map(e -> Integer.parseInt(currentPage)).orElse(1);
		int pageSize = 3;
		return goodsService.page(intCurrentPage, pageSize);
	}
	
	@Override
	public void destroy() {
		System.out.println("servlet销毁....");
		super.destroy();
	}
}
